package com.gxa.p2p.controller;


import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.BankCard;
import com.gxa.p2p.service.BankCardService;
import com.gxa.p2p.util.MapUtil;
import com.gxa.p2p.util.ReturnMap;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@Transactional
@RequestMapping("/BankCardController")
@Api(value = "银行卡管理控制器", tags = "银行卡管理模块")
public class BankCardController {
    @Resource(name = "bankCardServiceImpl")
    BankCardService bankCardService;

    @ResponseBody
    @PostMapping("/saveBankCard")
    public Map<String, Object> saveBankCard(@RequestBody BankCard bankCard) throws TencentCloudSDKException {
        MapUtil mapUtil = new MapUtil();
        if (bankCardService.saveBankCard(bankCard)) {
            mapUtil.setCode(0);
            mapUtil.setMsg("添加成功！！");
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    @GetMapping("/findCard")
    @ResponseBody
    @ApiOperation("根据用户Id查询当前用户绑定的银行卡")
    public Map<String, Object> findCard(@RequestParam("userId") Integer id){
        List<BankCard> userCard = bankCardService.findUserCard(id);
        ReturnMap<Object> objectReturnMap = new ReturnMap<>();
        return objectReturnMap.returnMap(userCard);
    }

    /**
     * 通过userId提供该用户银行卡信息
     * @param userId 用户编号
     * @return map
     */
    @GetMapping
    @ResponseBody
    public Map<String, Object> checkBankCard(@RequestParam("userId") Integer userId) {
        MapUtil mapUtil = new MapUtil();
        List<BankCard> bankCards = bankCardService.checkBankCard(userId);
        if (bankCards.size() == 0) {
            mapUtil.setCode("1");
            mapUtil.setMsg("尚未绑定银行卡");
        } else {
            mapUtil.setCode("0");
            mapUtil.setData(bankCards);
            mapUtil.setMsg("");
        }
        return mapUtil.getMap();

    }
}
